﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Imaging;

namespace LG.Common
{
    public static class ImageHelper
    {
        /// <summary>
        /// 判断文件是否有图片的后缀名
        /// </summary>
        /// <param name="fileName">文件</param>
        /// <returns></returns>
        public static bool IsImage(string fileName)
        {
            string[] parts = fileName.Split('.');
            if (parts.Length < 2)
                return false;
            string ext = parts[parts.Length - 1];
            switch (ext.ToUpper())
            {
                case "GIF":
                case "JPEG":
                case "JPG":
                case "BMP":
                case "PNG":
                case "ICO":
                    return true;
                default:
                    return false;
            }
        }

        /// <summary>
        /// 得到图片格式
        /// </summary>
        /// <param name="ext">图片后缀 如: gif或.gif</param>
        /// <returns></returns>
        public static ImageFormat GetFormate(string ext)
        {
            switch (ext.ToUpper())
            {
                case ".GIF":
                case "GIF":
                    return ImageFormat.Gif;
                case "JPEG":
                case "JPG":
                case ".JPEG":
                case ".JPG":
                    return ImageFormat.Jpeg;
                case ".BMP":
                case "BMP":
                    return ImageFormat.Bmp;
                case ".PNG":
                case "PNG":
                    return ImageFormat.Png;
                case ".ICO":
                case "ICO":
                    return ImageFormat.Icon;
                default:
                    throw new Exception("无效的图片格式！");
            }
        }

        /// <summary>
        /// 得到缩略图
        /// </summary>
        /// <param name="img">图片对象</param>
        /// <param name="maxWidth">最大宽度</param>
        /// <param name="maxHeight">最大高度</param>
        /// <param name="isAutoSize">是否在最大高宽中自适大小</param>
        /// <returns></returns>
        public static System.Drawing.Image GetThumbImage(System.Drawing.Image img, int maxWidth, int maxHeight, bool isAutoSize)
        {
            int tWidth;
            int tHeight;
            if (!isAutoSize)
            {
                tWidth = maxWidth;
                tHeight = maxHeight;
            }
            else
            {
                if (!(maxHeight > img.Height && maxWidth > img.Width))
                {
                    float HeightMultipier = (float)maxHeight / (float)img.Height;
                    float WidthMultipier = (float)maxWidth / (float)img.Width;
                    if (HeightMultipier > 1) HeightMultipier = 1;
                    if (WidthMultipier > 1) WidthMultipier = 1;
                    float SizeMultiplier = WidthMultipier < HeightMultipier ? WidthMultipier : HeightMultipier;
                    tWidth = (int)(img.Width * SizeMultiplier);
                    tHeight = (int)(img.Height * SizeMultiplier);
                }
                else
                {
                    tWidth = img.Width;
                    tHeight = img.Height;
                }
            }
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(tWidth, tHeight);
            //新建一个画板
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            //设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //清空画布并以透明背景色填充
            g.Clear(System.Drawing.Color.Transparent);
            //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(img, new System.Drawing.Rectangle(0, 0, tWidth, tHeight),
                new System.Drawing.Rectangle(0, 0, img.Width, img.Height),
                System.Drawing.GraphicsUnit.Pixel);
            return bitmap;
        }

        /// <summary>
        /// 得到剪切图
        /// </summary>
        /// <param name="oldImage"></param>
        /// <param name="x">右偏移</param>
        /// <param name="y">下偏移</param>
        /// <param name="width">剪切宽度</param>
        /// <param name="height">剪切高度</param>
        /// <returns></returns>
        public static System.Drawing.Image GetCuteImage(System.Drawing.Image img, int x, int y, int width, int height)
        {
            width = Math.Min(img.Width - x, width);
            height = Math.Min(img.Height - y, height);
            System.Drawing.Image newBitmap = new System.Drawing.Bitmap(width, height);
            using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(newBitmap))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.DrawImage(img, new System.Drawing.Rectangle(0, 0, width, height), new System.Drawing.Rectangle(x, y, width, height), System.Drawing.GraphicsUnit.Pixel);
                g.Save();
            }
            return newBitmap;
        }
    }
}
